//
//  M1CertifyPhotoCell.m
//  NewMarryU
//
//  Created by linl on 2018/7/19.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1CertifyPhotoCell.h"

#import "MJExtension.h"

@interface M1CertifyPhotoCell ()

/** 名字 */
@property (nonatomic, strong) UILabel *m1_lblName;
/** 删除按钮 */
@property (nonatomic, strong) UIButton *m1_btnDel;

@end

@implementation M1CertifyPhotoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.m1_imgHead];
        [self.contentView addSubview:self.m1_lblName];
        [self.contentView addSubview:self.m1_btnDel];
        
        [self makeConstraintSubviews];
    }
    return self;
}

#pragma mark - lazy load
- (UIImageView *)m1_imgHead
{
    if (!_m1_imgHead) {
        _m1_imgHead = [UIImageView new];
        _m1_imgHead.clipsToBounds = YES;
        _m1_imgHead.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _m1_imgHead;
}

- (UILabel *)m1_lblName
{
    if (!_m1_lblName) {
        _m1_lblName = [UILabel new];
        _m1_lblName.backgroundColor = [UIColor clearColor];
        _m1_lblName.textAlignment = NSTextAlignmentCenter;
        _m1_lblName.textColor = [UIColor whiteColor];
        _m1_lblName.font = [UIFont systemFontOfSize:12];
    }
    return _m1_lblName;
}

- (UIButton *)m1_btnDel
{
    if (!_m1_btnDel) {
        _m1_btnDel = [UIButton new];
        [_m1_btnDel setImage:[UIImage imageNamed:@"cer_del"] forState:UIControlStateNormal];
        [_m1_btnDel addTarget:self action:@selector(doDelete:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m1_btnDel;
}

//- (void)setIsShowWaterMark:(BOOL)isShowWaterMark
//{
//    isShowWaterMark ? [self.imgHead showMYWatermark] : [self.imgHead hideWatermark];
//}

- (void)setM1_isShowDeleteButton:(BOOL)isShowDeleteButton
{
    self.m1_btnDel.hidden = !isShowDeleteButton;
}

- (void)setM1_exampleString:(NSString *)exampleString
{
    self.m1_lblName.hidden = [exampleString length] == 0;
    
    self.m1_lblName.text = exampleString;
}

#pragma mark - private method
- (void)makeConstraintSubviews
{
    
    self.m1_imgHead.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 30);
    self.m1_lblName.frame = CGRectMake(0, CGRectGetMaxY(self.m1_imgHead.frame) + 5, self.frame.size.width, 25);
    self.m1_btnDel.frame = CGRectMake(self.frame.size.width - 20, 0, 20, 20);
    
}

#pragma mark - public method
- (void)m1_refreshCell:(NSArray *)photos des:(NSDictionary *)dic
{
//    self.clipsToBounds = NO;
//    
//    self.m1_imgHead.layer.borderColor = [m1_color(0xc2c2c2) CGColor];
//    
//    self.m1_imgHead.layer.borderWidth = 0.5;
    
    if (self.m1_selectIndex.item == [photos count])
    {
        if ([photos count] <= self.m1_maxPhoto)
        {
//            self.m1_imgHead.layer.borderColor = [[UIColor clearColor] CGColor];
            
            self.m1_imgHead.contentMode = UIViewContentModeCenter;
            
            self.m1_imgHead.image = [UIImage imageNamed:@"cer_add"];
            
            self.m1_isShowWaterMark = NO;
            
            self.m1_isShowDeleteButton = NO;
            
            self.m1_lblName.backgroundColor = m1_color(0xDBB76B);
            
            self.m1_lblName.text = dic[@"des2"];
        }
    }
    else
    {
        self.m1_imgHead.contentMode = UIViewContentModeScaleAspectFill;
        
        if (self.m1_selectIndex.item == 0) {
            self.m1_imgHead.image = [UIImage imageNamed:dic[@"img"]];
            self.m1_isShowDeleteButton = NO;
            self.m1_lblName.text = dic[@"des1"];
            self.m1_lblName.backgroundColor = m1_color(0xCDD6E0);
        } else {
            id data = photos[self.m1_selectIndex.item];
            if ([data isKindOfClass:[UIImage class]]) {
                self.m1_imgHead.image = data;
            } else if ([data isKindOfClass:[NSString class]]) {
                [self.m1_imgHead sd_setImageWithURL:[NSURL URLWithString:data]];
            }
            self.m1_isShowDeleteButton = YES;
            self.m1_lblName.text = dic[@"des2"];
            self.m1_lblName.backgroundColor = m1_color(0xDBB76B);
        }
    }
    
}

- (void)doDelete:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(certifyPhotoCell:didClickDeleteButton:)])
    {
        [self.delegate certifyPhotoCell:self didClickDeleteButton:sender];
    }
}

@end
